/**
 * 
 */
package com.huniu.analysis.hive;

import java.io.File;
import java.io.FileFilter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.huniu.analysis.model.QualityXMLProperty;
import com.huniu.links.model.LinkXMLProperty;
import com.huniu.utility.DateUtil;

/**
 * @author zandy (chenheng)
 * 
 */
public class HiveAnalyseCenterImpl implements IHiveAnalyseCenter {

	private static Logger logger = Logger
			.getLogger(HiveAnalyseCenterImpl.class);
	private String linkFolder;
	private String nectarFolder;
	private String qualityFolder ;
	private IScoutBeeAnalysePool scoutPool;
	private ILeaderBeeAnalysePool leaderPool ;

	public void setLinkFolder(String linkFolder) {
		this.linkFolder = linkFolder;
	}

	public void setNectarFolder(String nectarFolder) {
		this.nectarFolder = nectarFolder;
	}

	public void setQualityFolder(String qualityFolder) {
		this.qualityFolder = qualityFolder;
	}

	public void setScoutPool(IScoutBeeAnalysePool scoutPool) {
		this.scoutPool = scoutPool;
	}

	public void setLeaderPool(ILeaderBeeAnalysePool leaderPool) {
		this.leaderPool = leaderPool;
	}

	private File[] loadDictionaryFiles() {
		logger
				.info("Load dictionary files by path :"
						+ nectarFolder);
		File resFolder = new File(nectarFolder);
		File[] dicFiles = resFolder.listFiles(new FileFilter() {
			public boolean accept(File file) {
				if (file.getName().endsWith(DICTIONARY_EXTENSION)) {
					return true;
				}
				return false;
			}
		});
		logger.info("Available dictionary files :" + dicFiles.length);
		return dicFiles;
	}

	private List<File> loadLinkFiles() {
		logger.info("Load link folders by path :" + linkFolder);
		File[] availableFolders = getAvailableFolder(linkFolder,
				LINK_FOLDER_ANALYSE_PERIOD);
		logger.info("Available link folders : " + availableFolders.length);
		final List<File> linkFileList = new ArrayList<File>();
		for (File folder : availableFolders) {
			File[] linkFiles = folder.listFiles(new FileFilter() {
				public boolean accept(File file) {
					if (file.getName().endsWith(
							LinkXMLProperty.PROCESSED_EXTENSION)) {
						linkFileList.add(file);
						return true;
					}
					return false;
				}
			});
			logger.info("Available link files : " + linkFiles.length);
		}
		if (linkFileList.isEmpty()) {
			logger.info("No available link files !");
		}
		return linkFileList ;
	}
	
	private List<File> loadQualityFiles() {
		logger.info("Load quality folders by path : " + qualityFolder);
		File[] availableFolders = getAvailableFolder(qualityFolder,QUALITY_FOLDER_ANALYSE_PERIOD);
		logger.info("Available quality folders :" + availableFolders.length);
		final List<File> qualityFileList = new ArrayList<File>() ;
		for ( File folder : availableFolders ) {
			folder.listFiles(new FileFilter() {
				public boolean accept(File file) {
					if ( file.getName().endsWith(QualityXMLProperty.EXTENSION)) {
						qualityFileList.add(file);
						return true ;
					}
					return false ;
				}
			});
		}
		return qualityFileList ;
	}

	private File[] getAvailableFolder(String parentFolder, final int period) {
		logger.info("Load available folders by path :" + parentFolder);
		File folder = new File(parentFolder);
		File[] folders = folder.listFiles(new FileFilter() {
			public boolean accept(File folder) {
				String folderName = folder.getName();
				logger.info("Folder name : " + folderName);
				try {
					String strCurrentDate = DateUtil.convertDateToString(
							new Date(), DateUtil.DATE_FORMAT_1);
					logger.info("Current Date : " + strCurrentDate);
					long duration = DateUtil.calculateDuration(strCurrentDate,
							folderName, DateUtil.DATE_FORMAT_1,
							DateUtil.DATE_FORMAT_1);
					logger.info("Duration between " + folderName + " and "
							+ strCurrentDate + " is " + duration);
					if (duration > period) {
						logger
						.info("Duration is greater than period (duration("
								+ duration
								+ ") - period("
								+ period
								+ ") = " + (duration - period) + ")");
						return false;
					}
					return true;
				} catch (ParseException e) {
					logger.error("Available folder(" + folderName + "),just ignore this folder");
				}
				return false;
			}
		});
		logger.info("Available folders : " + folders.length );
		return folders;
	}

	public void startScoutPoolAnalyse() {
		File[] dicFiles = loadDictionaryFiles();
		List<File> linkFiles = loadLinkFiles();
		if ( linkFiles != null ) {
			scoutPool.setDictionaryFiles(dicFiles);
			scoutPool.setLinkFiles(linkFiles);
			scoutPool.setQualityFolder(qualityFolder);
			scoutPool.analyse();
		}
	}
	
	public void startHeaderPoolAnalse() {
		List<File> qualityFiles = loadQualityFiles();
		if ( qualityFiles != null ) {
			leaderPool.setQualityFiles(qualityFiles);
			leaderPool.analyse();
		}
	}
}
